package com.android.sgzcommon.activity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.android.sgzcommon.utils.BitmapUtils;
import com.android.sgzcommon.view.DragPhotoView;
import com.android.sugz.R;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;

import java.io.File;

public class PhotoViewActivity extends BaseActivity {

    private DragPhotoView mDpvImage;
    private ImageView mIvBack;
    private ProgressBar mPbLoading;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sgz_photo_view);
        String path = getIntent().getStringExtra("path");
        if (!TextUtils.isEmpty(path)) {
            if (path.startsWith("http")) {
                mImageLoader.get(path, new ImageLoader.ImageListener() {
                    @Override
                    public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
                        Bitmap bitmap = response.getBitmap();
                        if (bitmap != null){
                            mPbLoading.setVisibility(View.GONE);
                            mDpvImage.setImageBitmap(bitmap);
                        }else {
                            mPbLoading.setVisibility(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        mPbLoading.setVisibility(View.GONE);
                        mDpvImage.setImageResource(R.drawable.ic_sgz_img_load_failure);
                    }
                }, 1920, 1920);
            } else {
                mPbLoading.setVisibility(View.GONE);
                File file = new File(path);
                if (file.exists()) {
                    mDpvImage.setImageBitmap(BitmapUtils.getWindowFitBitmap(this, path));
                }
            }
        }
        mDpvImage.setOnTouchListener(new DragPhotoView.OnTouchListener() {
            @Override
            public void onTap(DragPhotoView view) {

            }

            @Override
            public void onMovePercent(DragPhotoView view, float percent) {
                Log.d("PhotoViewActivity", "onMovePercent: percent == " + percent);
                mIvBack.setAlpha(percent);
            }

            @Override
            public void onExit(DragPhotoView view, float translateX, float translateY, float width, float height) {
                finish();
            }
        });
    }

    @Override
    public void onContentChanged() {
        mDpvImage = findViewById(R.id.dpv_image);
        mIvBack = findViewById(R.id.iv_back);
        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mPbLoading = findViewById(R.id.pb_loading);
        super.onContentChanged();
    }
}